package org.je.log;

import java.io.File;
import java.io.InputStream;

import org.je.cons.ClassPath;
import org.je.cons.JCons;
import org.je.ufo.PUtil;
import org.je.util.DynBuffer;
import org.je.util.StringUtil;

/**
 * @author fanyifeng
 */
class LogPath
{
	private static String			path;
	private static volatile boolean	isInit		= false;
	private static final Object		initLock	= new Object();

	public static String get() {
		init();
		return path;
	}

	private static void init() {

		if (isInit)
			return;
		synchronized(initLock) {
			if (isInit)
				return;
			doInit();
			isInit = true;
		}
	}

	private static void doInit() {
		try {
			InputStream in = LogPath.class.getResourceAsStream("/log.path");
			byte[] tem = new byte[128];
			DynBuffer bf = new DynBuffer();
			while (true) {
				int len = in.read(tem);
				if (len < 0) {
					break;
				}
				bf.append(tem, 0, len);
			}
			in.close();
			String fileText = new String(bf.toArray(), JCons.enc().utf8).trim();
			String url = StringUtil.replace(fileText, "#classpath#", ClassPath.get());
			path = PUtil.format(url);
		} catch (Throwable ex) {
			path = ClassPath.get() + "/logs";
		}
		try {
			File f = new File(path);
			if (f.exists() == false) {
				f.mkdirs();
			}
		} catch (Throwable ex) {}
	}

}
